package com.lilanz.facepass.controller.ShopBrand;


import com.alibaba.fastjson.JSONObject;
import com.lilanz.facepass.entity.client.Client;
import com.lilanz.facepass.entity.exception.MyException;
import com.lilanz.facepass.service.impl.ClientServiceImpl;
import com.lilanz.facepass.service.impl.HTTPServiceImpl;
import com.lilanz.facepass.util.Json;
import com.lilanz.facepass.util.ToolUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/shopBrand")
@PropertySource({"classpath:application.properties"})//引入外部资源文件
public class ShopBrandController {

    @Value("${shop-brand.get-city-info}")
    private String getCityInfo;

    @Value("${shop-brand.get-shop-list}")
    private String getShopList;

    @Resource
    private ClientServiceImpl clientService;
    @Resource
    private HTTPServiceImpl httpService;

    @GetMapping("/getCityInfo")
    public JSONObject getCityInfo() {
        Client client = clientService.selectClient();
        Map<String, String> map = new HashMap<>();
        map.put("phoneNo", client.getPhoneNo());
        map.put("password", client.getPassword());
        map.put("vi", client.getVi());
        map.put("key", ToolUtil.getKey(map));//这个key一定放最后一个

        JSONObject result =httpService.sendPost(getCityInfo, client.getToken(), map);
        return Json.success(0, "ok", result);
    }


    @PostMapping("/getShopList")
    public JSONObject getShopList(@RequestParam String parentcode, String areaname, String province, String city) {
        Client client = clientService.selectClient();
        Map<String, String> map = new HashMap<>();
        map.put("phoneNo", client.getPhoneNo());
        map.put("password", client.getPassword());
        map.put("vi", client.getVi());
        map.put("parentcode", parentcode);
        if (areaname != null) {
            map.put("areaname", parentcode);
        }
        if (areaname != null) {
            map.put("province", province);
        }
        if (areaname != null) {
            map.put("city", city);
        }
        map.put("key", ToolUtil.getKey(map));

        JSONObject result =httpService.sendPost(getShopList, client.getToken(), map);
        return Json.success(0, "ok", result);
    }


}
